package com.iccct.cloud.user.service.impl;

import com.iccct.cloud.user.model.resp.MenuResp;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.iccct.cloud.user.model.Menu;
import com.iccct.cloud.user.mapper.MenuMapper;
import com.iccct.cloud.user.service.MenuService;

import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @author iccct (taowutt1@gmail.com)
 * @since 2021-11-08  14:29
 */
@Service
public class MenuServiceImpl implements MenuService{

    @Resource
    private MenuMapper menuMapper;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return menuMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Menu record) {
        return menuMapper.insert(record);
    }

    @Override
    public int insertSelective(Menu record) {
        return menuMapper.insertSelective(record);
    }

    @Override
    public Menu selectByPrimaryKey(Long id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Menu record) {
        return menuMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Menu record) {
        return menuMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<MenuResp> getMenuList() {
        List<MenuResp> menus = menuMapper.queryList();

        List<MenuResp> collect = menus.stream()
                .filter(v -> v.getParentId() == 0)
                .map(v -> {
                    List<MenuResp> menuResps = menus.stream().filter(s -> s.getParentId() == v.getId()).collect(Collectors.toList());
                    v.setChildrenList(menuResps);
                    return v;
                })
                .collect(Collectors.toList());

        return collect;
    }

}
